#include "cc_event_thread.h"

#if (CC_CONFIG_TEST_EVENT_THREAD == 1)

enum
{
  CC_EVENT1 = 1,
  CC_EVENT2,
  CC_EVENT3
};

struct student
{
  int age;
};
void cc_topic_notify(cc_event_thread_t ev_th, cc_mnode_t node)
{
  cc_event_msg_t msg = (cc_event_msg_t)node->mem;
  cc_log("event port(%d) recv port(%d) event(%d) topic(%s)\r\n", ev_th->port_id, node->src_port_id, msg->event,msg->topic);
  cc_log("data(%s)\r\n", msg->data);
  return;
}

void cc_event1_notify(cc_event_thread_t ev_th, cc_mnode_t node)
{
  cc_event_msg_t msg = (cc_event_msg_t)node->mem;
  cc_log("event port(%d) recv port(%d) event(%d) arg:%s\r\n", ev_th->port_id, node->src_port_id, msg->event, msg->data);
  return;
}

void cc_event2_notify(cc_event_thread_t ev_th, cc_mnode_t node)
{
  cc_event_msg_t msg = (cc_event_msg_t)node->mem;
  cc_log("event port(%d) recv port(%d) event(%d) arg:%s\r\n", ev_th->port_id, node->src_port_id, msg->event, msg->data);
  return;
}

void cc_event3_notify(cc_event_thread_t ev_th, cc_mnode_t node)
{
  cc_event_msg_t msg = (cc_event_msg_t)node->mem;
  struct student *stu = (struct student *)msg->data;
  cc_log("event port(%d) recv port(%d) event(%d) stu_data is %d\r\n", ev_th->port_id, node->src_port_id, msg->event, stu->age);

  return;
}

void event_thread_test1(void *data)
{
  int cnt = 0;
  cc_event_thread_t evth = cc_event_thread_create(CC_PORT_EVENT_TEST1, 5, 2048, 10, CC_EVENT_THREAD_MSG_SIZE(100), cc_topic_notify);
  cc_event_thread_subscribe(evth, "hello");
  cc_event_thread_notify_attach(evth, CC_EVENT1, cc_event1_notify);
  cc_event_thread_notify_attach(evth, CC_EVENT2, cc_event2_notify);
  cc_event_thread_start(evth);
  while (1)
  {
    cc_delay(1000);
    // cnt++;
    // if (cnt == 5)
    // {
    //   cc_event_thread_notify_detach(evth, CC_EVENT2);
    // }
    // if (cnt == 10)
    // {
    //   cc_event_thread_suspend(evth);
    // }
    // if (cnt == 15)
    // {
    //   cc_event_thread_unsubscribe(evth, "hello");
    // }
    // if (cnt == 20)
    // {
    //   cc_event_thread_start(evth);
    // }
  }
}
CC_THREAD_REGISTER(event_thread_test1, 7, 3072);

void event_thread_test3(void *data)
{
  int cnt = 0;
  cc_event_thread_t evth = cc_event_thread_create(CC_PORT_EVENT_TEST3, 5, 2048, 10, CC_EVENT_THREAD_MSG_SIZE(100), cc_topic_notify);
  cc_event_thread_subscribe(evth, "hello");
  cc_event_thread_notify_attach(evth, CC_EVENT3, cc_event3_notify);
  cc_event_thread_start(evth);
  while (1)
  {
    cc_delay(1000);
  }
}
CC_THREAD_REGISTER(event_thread_test3, 7, 3072);

void event_thread_test2(void *data)
{
  cc_event_thread_t evth = cc_event_thread_create(CC_PORT_EVENT_TEST2, 5, 2048, 10, CC_EVENT_THREAD_MSG_SIZE(100), CC_NULL);
  cc_event_thread_start(evth);
  int cnt;
  int age;
  while (1)
  {
    struct student stu;
    if (cnt == 1 && CC_EOK == cc_event_thread_send_data(evth, CC_PORT_EVENT_TEST1, CC_EVENT1, CC_NULL, 0))
    {
      cc_log_d("send success to port(%d) event(%d)\r\n", CC_PORT_EVENT_TEST1, CC_EVENT1);
    }

    if (cnt == 2)
    {
      cc_event_thread_send_data(evth, CC_PORT_EVENT_TEST3, CC_EVENT3, &stu, sizeof(struct student));
      stu.age = age++;
      cc_log_d("send success to port(%d) event(%d)\r\n", CC_PORT_EVENT_TEST3, CC_EVENT3);
    }

    if (cnt == 3 && CC_EOK == cc_event_thread_send_data(evth, CC_PORT_EVENT_TEST1, CC_EVENT2, (void *)"hello world", strlen("hello world") + 1))
    {
      cc_log_d("send success\r\n");
    }
    if (cnt == 4)
    {
      cc_event_thread_publish(evth, "hello", (void *)"hello publish", strlen("hello publish") + 1);
    }
    if (cnt++ == 5)
    {
      cc_event_thread_broadcast(evth, (void *)"hello broadcast", strlen("hello broadcast") + 1);
      cnt = 0;
    }
    cc_delay(1000);
  }
}
CC_THREAD_REGISTER(event_thread_test2, 7, 3072);
#endif
